package com.fourth.shopping.service.impl;

import com.fourth.entity.Address;
import com.fourth.entity.OrderAddress;
import com.fourth.shopping.commons.OrdersTableSearch;
import com.fourth.shopping.commons.TableData;
import com.fourth.shopping.dao.AddressMapper;
import com.fourth.shopping.dao.OrderAddressMapper;
import com.fourth.shopping.dao.OrdersMapper;
import com.fourth.shopping.service.OrderService;
import com.fourth.vo.OrdersVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrdersMapper ordersMapper;
    @Resource
    private AddressMapper addressMapper;
    @Resource
    private OrderAddressMapper orderAddressMapper;

    @Override
    public TableData<OrdersVO> getOrderAsPage(OrdersTableSearch ordersTableSearch, Integer offset, Integer limit) {
        List<OrdersVO> orderAsPage = ordersMapper.getOrderAsPage(ordersTableSearch, offset, limit);
        return new TableData<OrdersVO>(orderAsPage,getOrderNum(ordersTableSearch));
    }

    @Override
    public Integer getOrderNum(OrdersTableSearch ordersTableSearch){
        return ordersMapper.getOrdersNum(ordersTableSearch);
    }

    @Override
    @Transactional
    public Boolean modifyOrdersAddress(String ono, Address address) {
        /*
        * 如果是已存在地址，则直接使用该地址
        * 否则根据当前地址，查询是否有已存在地址
        * 如果有，则获取已存在地址
        * 如果没有，则将地址插入数据库
        *
        * 最终address一定存在且在数据库中有记录
        * */
        //如果是历史地址
        if(address.getAId() == null){
            //查询是否已存在该地址
            List<Address> addressList = addressMapper.queryByLimit(address);
            //如果已存在，直接使用该地址
            if(addressList != null && addressList.size() > 0){
                address = addressList.get(0);
                address.setATakeDeliveryState(1);
                addressMapper.update(address);
            }
            //如果不存在，新增该地址
            else{
                address.setUId(ordersMapper.getUIdByOrdersNo(ono).get(0));
                address.setAState(0);
                address.setATakeDeliveryState(1);
                addressMapper.insert(address);
                address = addressMapper.queryByLimit(address).get(0);
            }
        }

        /*
        *
        * 称当前订单号对应的订单为当前订单；
        * 获取当前订单对应的收货地址
        * 如果当前订单已存在收货地址，则进行修改
        * 如果当前订单不存在收货地址，则进行插入
        *
        * */

        //获取当前订单对应的地址
        OrderAddress orderAddress = orderAddressMapper.queryByOrderNo(ono);

        //如果当前订单不存在收货地址
        if(orderAddress.getId() == null){
            orderAddress = new OrderAddress();
            orderAddress.setOAddress(address.getAId());
            orderAddress.setONo(ono);
            orderAddressMapper.insert(orderAddress);
            return orderAddressMapper.queryByOrderNo(ono).getId() != null;
        }

        //如果当前订单已存在收货地址
        orderAddress.setOAddress(address.getAId());
        orderAddressMapper.update(orderAddress);
        return true;
    }

}
